<template>
  <basic-container>
    <div class="card-border">
      <card-header :header-data="headerData"></card-header>
      <avue-crud :data="levyResultData"
                 :option="tableoption"
                 :page="page"
                 :table-loading="tableLoading"
                 @size-change="sizeChange"
                 @current-change="currentChange"
                 @search-change="searchChange"
                 @search-reset="searchReset"
                 class="avuecrud">
        <template slot="menuRight">
          <el-button type="warning" size="small" @click="exportToDoConnectionData()">导出</el-button>
        </template>
        <template slot-scope="scope" slot="menu">
          <div class="menu-title" style="padding: 9.5px 0px">
            <i class="iconfont  icon-primary"></i>
            <p class="menu-title-text"></p>
          </div>
          <div class="menu-title" @click.stop="collectionFeedback(scope.row,scope.index)" v-if="scope.row.state=='1'">
            <i class="iconfont iconalter icon-warning"></i>
            <p class="menu-title-text">反馈</p>
          </div>
          <div class="menu-title" @click.stop="showCandidateManage(scope.row.targetId)" v-if="scope.row.state=='1'">
            <i class="iconfont iconalter icon-warning"></i>
            <p class="menu-title-text">转办</p>
          </div>
        </template>
      </avue-crud>
      <el-dialog title="反馈" :visible.sync="dialogVisible" width="70%">
        <el-form
          label-width="130px"
          :model="projectForm"
          style="padding-right: 30px;"
          :rules="hasOpinionFlag?opinionRules:notOpinionRules"
          ref="projectForm"
        >
          <el-row>
            <el-col :span="11">
              <el-form-item class="form-item-release" label="意见类型 :" prop="hasOpinion">
                <!-- <el-radio v-model="projectForm.hasOpinion" label="0">无意见</el-radio> -->
                <el-radio v-model="projectForm.hasOpinion" label="1">衔接意见</el-radio>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="22">
              <el-form-item class="form-item-release" label="衔接意见 :" prop="opinion">
                <el-input
                  type="textarea"
                  :autosize="{ minRows: 4, maxRows: 4}"
                  placeholder="请输入衔接意见"
                  v-model="projectForm.opinion"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="11">
              <el-form-item class="form-item-release" label="联系人 :" prop="contactName">
                <el-input
                  v-model="projectForm.contactName"
                  placeholder="请输入联系人"
                  style="width: 280px"
                >
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="11">
              <el-form-item class="form-item-release" label="联系电话 :" prop="contactNumber">
                <el-input
                  v-model="projectForm.contactNumber"
                  placeholder="请输入联系电话"
                  style="width: 280px"
                >
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div class="form-group-file">
          <span class="form-label-file">衔接材料 :</span>
          <el-upload
            class="upload-demo"
            ref="uploadrelated"
            action="string"
            :http-request="submitUploadMixin"
            :on-preview="handlePreviewMixin"
            :on-remove="handleRemoveMixin"
            :file-list="fileListMixin"
            :auto-upload="true"
            :on-change="fileChangeMixin">
            <el-button slot="trigger" size="small" type="primary">点击上传</el-button>
            <!--<div slot="tip" class="el-upload__tip">上传工作方案、工作计划等相关材料</div>-->
          </el-upload>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="commitYes">提 交</el-button>
            <el-button @click="dialogVisible = false">关 闭</el-button>
        </span>
      </el-dialog>
    </div>
    <candidate-manage :visible="candidateManageVisible" :after-close="closeCandidateManage" :target-id="currentTargetId"></candidate-manage>
  </basic-container>
</template>

<script>
  import {connectionFeedback, getPlanConnectionSingle} from "@/api/make";
  import {makeConnectFormOpinion,exportToDoConnectionData,exportToDoConnectionDataExcel} from "@/api/opinion";
  import {getNotNull} from '@/views/util';
  import {getfileinfopage} from "@/api/system"
  import relatedfile from "@/mixins/relatedfile";
  import {collectionFeedbackRules} from "@/const/infoRules";
  import getDetailData from "@/mixins/getDetailData";
  import {resultDetail} from "@/api/planning/result";
  import {opinionTagertList} from "@/api/opinion"
  import {toDoTargetOpinion} from "@/api/review"
  import candidateManage from "@/components/opinion/candidateManage.vue";

  export default {
    name: "todoConnection",
    mixins: [relatedfile, getDetailData()],
    components:{
      candidateManage
    },
    created() {
      this.getResultList(this.page)
    },
    data() {
      return {
        headerData: {
          title: '规划衔接'
        },
        levyResultData: [],
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 10 // 每页显示多少条
        },
        tableoption: {
          page: true,
          menuWidth: 160,
          addBtn: false,
          editBtn: false,
          addRowBtn: false,
          indexLabel: "序号",
          index: true,
          cellBtn: false,
          indexWidth:70,
          delBtn: false,
          columnBtn: false,
          refreshBtn: false,
          filterBtn: false,
          searchBtn: false,
          keyId: "id",
          column: [{
            label: "衔接主题",
            prop: "title",
            search: true,
            cell: true
          },
            {
              label: "发起时间",
              width: 110,
              prop: "createTime",
              cell: true,
              formatter: function (row, value, label, column) {
                return value.split(' ')[0]
              }
            },
            {
              label: "反馈部门",
              width: 120,
              prop: "targetDeptName",
              cell: true
            },
            {
              label: "状态",
              width: 80,
              prop: "state",
              cell: true,
              formatter: function (row, value, label, column) {
                switch (value) {
                  case '0':
                    return '催办';
                  case '1':
                    return '待反馈';
                  case '2':
                    return '退回';
                  case '3':
                    return '不采纳';
                  case '4':
                    return '已反馈';
                  case '5':
                    return '已采纳'
                }
              }
            },

          ]
        },
        tableLoading: false,
        dialogVisible: false,
        detailData: [],
        fileIdstemp: [],
        projectForm: {
          opinion: '',
          hasOpinion: '1',
        },
        collectionFeedbackRules: collectionFeedbackRules,
        activeNames: ['0'],
        hasOpinionFlag: true,
        opinionRules:{
          opinion: [
            {required: true, message: "请输入衔接意见", trigger: "blur"}
          ],
          contactName: [
            {required: true, message: "请输入联系人", trigger: "blur"}
          ],
          contactNumber: [
            {required: true, message: "请输入联系电话", trigger: "blur"}
          ],
        },
        notOpinionRules:{
          opinion: [
            {required: false}
          ],
          contactName: [
            {required: true, message: "请输入联系人", trigger: "blur"}
          ],
          contactNumber: [
            {required: true, message: "请输入联系电话", trigger: "blur"}
          ],
        },
        titleLikeSearch:'',
        currentTargetId: null,
        candidateManageVisible: false,
      }
    },
    watch: {
      'projectForm.hasOpinion'(val) {
        if (val == '1') {
          this.hasOpinionFlag = true
        }
        if (val == '0') {
          this.hasOpinionFlag = false
        }
      }
    },
    methods: {
      showCandidateManage(targetId){
        this.currentTargetId = targetId;
        this.candidateManageVisible = true;
      },
      closeCandidateManage(){
        this.candidateManageVisible = false;
        this.getResultList(this.page);
      },
      // 课题详情跳转
      collectionFeedback(row) {
        this.projectForm.targetId = row.targetId;
        this.dialogVisible = true;
      },

      currentChange(currentPage) {
        var page = this.page;
        page.currentPage = currentPage;
        let params = {
          titleLike:this.titleLikeSearch,
        };
        this.getResultList(page,params);
      },
      sizeChange(pageSize) {
        var page = this.page;
        page.pageSize = pageSize;
        let params = {
          titleLike:this.titleLikeSearch,
        };
        this.getResultList(page,params);
      },

      getResultList(page,params) {
        toDoTargetOpinion(Object.assign({
          current: page.currentPage,
          size: page.pageSize,
          type: '6',
          states:'0,1,2'
        },params)).then(response => {
          this.levyResultData = response.data.data.records;
          this.page.total = response.data.data.total;
          this.$emit("getTotal", response.data.data.total)
        })
      },
      searchReset() {
        this.titleLikeSearch = '';
        let params = {
          titleLike:this.titleLikeSearch,
        };
        this.getResultList(this.page,params);
      },
      //搜索回调
      searchChange (params) {
        this.page.currentPage = 1
        this.getResultList(this.page, this.dealSearchParam(params));
      },
      // 处理搜索过滤
      dealSearchParam(params) {
        if (params.title) {
          params.titleLike = params.title;
        }
        this.titleLikeSearch = params.title;
        return params;
      },
      commitYes() {
        this.$refs.projectForm.validate(valid => {
          if (valid) {
            this.$confirm("确定提交吗?", "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning"
            }).then(() => {
              //将多个相关材料的附件id拼成逗号隔开的字符串放在 this.projectForm.fileId 中
              var arr = [];
              for (var item in this.fileListMixin) {
                arr.push(this.fileListMixin[item].url);
              }
              this.projectForm.fileIds = arr;
              this.projectForm.hasOpinion = parseInt(this.projectForm.hasOpinion);
              makeConnectFormOpinion(this.projectForm).then(response => {
                if (response.data.code == 0) {
                  this.$notify({
                    title: "成功",
                    message: "发布成功",
                    type: "success",
                    duration: 2000
                  });
                  this.$router.go(0);
                }
              })
            })
          }
        })
      },
      exportToDoConnectionData(){
        this.$confirm("确定要导出规划衔接列表吗?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          exportToDoConnectionDataExcel(Object.assign({
            type: '6',
            states:'0,1,2'
          })).then(response => {
            let exportFileName = "规划衔接列表.xlsx";
            this.download(response.data, exportFileName)
          });
        })
      },
      // 下载文件
      download(data, filename) {
        if (data == null) {

        } else {
          let url = window.URL.createObjectURL(new Blob([data]));
          let link = document.createElement('a');
          link.style.display = 'none';
          link.href = url;
          link.setAttribute('download', filename);
          document.body.appendChild(link);
          link.click()
        }
      },
    }
  }
</script>

<style lang="scss" scoped>

</style>
